package com.elinkway.infinitemovies.utils;

import android.content.Intent;
import android.text.TextUtils;

import com.apkfuns.logutils.LogUtils;
import com.elinkway.infinitemovies.selfdata.MyDataRecordCreater;
import com.elinkway.infinitemovies.selfdata.bean.DeepLinkDataRecord;
import com.elinkway.infinitemovies.selfdata.dao.RealmDaoManager;

/**
 * Created by richard on 2017/7/10.
 */
public class DeepLinkUtil {

    public static class ActionType {
        public static final String HOME = "0";
        public static final String TOPIC = "1";
        public static final String OLD_VIDEO_DETAIL_NO_PLAY = "2";
        public static final String TOPIC_DETAIL = "3";
        public static final String RANK = "4";
        public static final String VIDEO_PLAY = "5";
        public static final String H5 = "6";
    }

    public static class VideoType {
        public static final String TV = "1";
        public static final String FILM = "2";
        public static final String CARTOON = "3";
        public static final String VARIETY = "4";
        public static final String DOCUMENTARY = "16";
    }

    public static final String ACTION_TYPE = "actionType";
    public static final String VIDEO_ID = "videoId";
    public static final String VIDEO_TYPE = "videoType";
    public static final String NAME = "name";
    public static final String THEME_ID = "themeId";
    public static final String SITE = "site";
    public static final String EPISODE_NUMBER = "episodeNumber";
    public static final String URL = "url";
    public static final String TITLE = "title";
    public static final String SOURCE = "source";

    public static final String DEEP_LINK_ACODE = "36";

    public static void uploadDeepLink(Intent intent) {
        DeepLinkDataRecord deepLinkDataRecord = MyDataRecordCreater.build(DeepLinkDataRecord.class);
        if (deepLinkDataRecord != null && intent != null) {
            String actionType = intent.getStringExtra(DeepLinkUtil.ACTION_TYPE);
            String source = intent.getStringExtra(DeepLinkUtil.SOURCE);
            deepLinkDataRecord.setSource(source);
            LogUtils.d("upload actionType:" + actionType + " source:" + source);
            if (!TextUtils.isEmpty(source)) {
                deepLinkDataRecord.setAcode(DEEP_LINK_ACODE);
                deepLinkDataRecord.setActionType(actionType);
                if (DeepLinkUtil.ActionType.TOPIC_DETAIL.equals(actionType)) {
                    String themeId = intent.getStringExtra(DeepLinkUtil.THEME_ID);
                    String name = intent.getStringExtra(DeepLinkUtil.NAME);
                    deepLinkDataRecord.setThemeId(themeId);
                    deepLinkDataRecord.setName(name);
                } else if (DeepLinkUtil.ActionType.OLD_VIDEO_DETAIL_NO_PLAY.equals(actionType)) {
                    String videoId = intent.getStringExtra(DeepLinkUtil.VIDEO_ID);
                    String videoType = intent.getStringExtra(DeepLinkUtil.VIDEO_TYPE);
                    String site = intent.getStringExtra(DeepLinkUtil.SITE);
                    String episodeNumber = intent.getStringExtra(DeepLinkUtil.EPISODE_NUMBER);
                    String name = intent.getStringExtra(DeepLinkUtil.NAME);
                    String themeId = intent.getStringExtra(DeepLinkUtil.THEME_ID);
                    deepLinkDataRecord.setVideoId(videoId);
                    deepLinkDataRecord.setVideoType(videoType);
                    deepLinkDataRecord.setSite(site);
                    deepLinkDataRecord.setEpisodeNumber(episodeNumber);
                    deepLinkDataRecord.setName(name);
                } else if (DeepLinkUtil.ActionType.RANK.equals(actionType)) {
                    String videoType = intent.getStringExtra(DeepLinkUtil.VIDEO_TYPE);
                } else if (DeepLinkUtil.ActionType.VIDEO_PLAY.equals(actionType)) {
                    String videoId = intent.getStringExtra(DeepLinkUtil.VIDEO_ID);
                    String videoType = intent.getStringExtra(DeepLinkUtil.VIDEO_TYPE);
                    String site = intent.getStringExtra(DeepLinkUtil.SITE);
                    String episodeNumber = intent.getStringExtra(DeepLinkUtil.EPISODE_NUMBER);
                    String name = intent.getStringExtra(DeepLinkUtil.NAME);
                    String themeId = intent.getStringExtra(DeepLinkUtil.THEME_ID);
                    deepLinkDataRecord.setVideoId(videoId);
                    deepLinkDataRecord.setVideoType(videoType);
                    deepLinkDataRecord.setSite(site);
                    deepLinkDataRecord.setEpisodeNumber(episodeNumber);
                    deepLinkDataRecord.setName(name);
                } else if (DeepLinkUtil.ActionType.H5.equals(actionType)) {
                    String url = intent.getStringExtra(DeepLinkUtil.URL);
                    String title = intent.getStringExtra(DeepLinkUtil.TITLE);
                    deepLinkDataRecord.setUrl(url);
                    deepLinkDataRecord.setTitle(title);
                } else if (DeepLinkUtil.ActionType.TOPIC.equals(actionType)) {

                }
                if (deepLinkDataRecord.getSource() != null) {
                    RealmDaoManager.addMyDataRecord(deepLinkDataRecord);
                }
            }
        }
    }
}
